package com.calvin.study.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Binding.DestinationType;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {

	// 秒杀活动结束队列
	@Bean
	public Queue seckillActEndQueue() {
		// 该队列绑定的死信交换机
		Map<String, Object> arguments = new HashMap<>();
		arguments.put("x-dead-letter-exchange", "dead-letter-exchange");
		arguments.put("x-dead-letter-routing-key", "deat.letter");
		
		return new Queue("seckill.act.and.queue", true, false, false, arguments);
	}

	// 死信队列
	@Bean
	public Queue deadLetterQueue() {
		return new Queue("dead.letter.queue", true, false, false);
	}

	@Bean
	public TopicExchange seckillEventExchange() {
		return new TopicExchange("seckill-event-exchange", true, false);
	}

	// 声明死信交换机
	@Bean
	public TopicExchange deadLetterDirectExchange() {
		return new TopicExchange("dead-letter-exchange", true, false);
	}

	@Bean
	public Binding placeOrderBinding() {
		return new Binding("seckill.act.and.queue", DestinationType.QUEUE, "seckill-event-exchange", "seckill.act.and",
				null);
	}

	// 死信交换机、队列绑定
	@Bean
	public Binding deadLetterBingds() {
		return BindingBuilder.bind(deadLetterQueue()).to(deadLetterDirectExchange()).with("deat.letter");
	}

}
